package com.hnas.trade.dao.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.persistence.Query;

import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;

import com.hnas.core.dao.BaseDaoImpl;
import com.hnas.core.util.DateUtil;
import com.hnas.core.util.StringUtil;
import com.hnas.trade.dao.TradeRecordDao;
import com.hnas.trade.dto.DTOTradeRecord;
import com.hnas.trade.orm.TradeRecord;

@Component("tradeRecordDao")
public class TradeRecordDaoImpl extends BaseDaoImpl<TradeRecord> implements TradeRecordDao {

	public TradeRecordDaoImpl() {
		super(TradeRecord.class);
	}

	@Override
	public TradeRecord saveOrUpdate(DTOTradeRecord dto) throws Exception {
		TradeRecord vo = null;
		String date = DateUtil.dateTimeToString(new Date());
		if (dto != null) {
			String id = dto.getId();
			if (StringUtil.isNotEmpty(id)) {
				vo = this.find(id);
			}
			if (vo == null) {// 新增
				vo = new TradeRecord();
				BeanUtils.copyProperties(dto, vo);
				vo.setCreateTime(date);
				vo.setUpdateTime(date);
				vo.setIsDeleted(true);
				this.persist(vo);
			} else {// 修改
				BeanUtils.copyProperties(dto, vo, new String[] { "createTime", "creator" });
				vo.setUpdateTime(date);
				this.merge(vo);
			}
		}
		return vo;
	}

	@Override
	public TradeRecord findBySwiftNumber(String swiftNumber) {
		List<TradeRecord> list = this.findByQuery("from TradeRecord t where t.swiftNumber = ?1 and t.isDeleted ='1' ",
				swiftNumber);
		if (null != list && list.size() > 0) {
			return list.get(0);
		}
		return null;
	}

	@Override
	public List<TradeRecord> getList(String account_id, String order_parent_type, String order_type, int pageNo,
			int pageSize) {
		StringBuffer sb = new StringBuffer("from TradeRecord t where t.isDeleted='1' ");
		int j = 1;
		List<Object> paras = new ArrayList<Object>();
		if (StringUtil.isNotEmpty(account_id)) {
			sb.append(" and t.accId = ?" + j++);
			paras.add(account_id);
		}
		if (StringUtil.isNotEmpty(order_parent_type)) {
			sb.append(" and t.orderParentType = ?" + j++);
			paras.add(order_parent_type);
		}
		if (StringUtil.isNotEmpty(order_type)) {
			sb.append(" and t.orderType = ?" + j++);
			paras.add(order_type);
		}
		sb.append(" order by t.tradeEndTime desc");
		Query query = this.entityManager.createQuery(sb.toString());
		for (int i = 1; i < paras.size() + 1; i++) {
			query.setParameter(i, paras.get(i - 1));
		}
		query.setFirstResult((pageNo - 1) * pageSize);
		query.setMaxResults(pageSize);
		try {
			return query.getResultList();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public List<TradeRecord> getList2(String account_id, String order_parent_type, String order_type, String date,
			int pageNo, int pageSize) {
		StringBuffer sb = new StringBuffer("from TradeRecord t where t.isDeleted='1' ");
		int j = 1;
		List<Object> paras = new ArrayList<Object>();
		if (StringUtil.isNotEmpty(account_id)) {
			sb.append(" and t.accId = ?" + j++);
			paras.add(account_id);
		}
		if (StringUtil.isNotEmpty(order_parent_type)) {
			sb.append(" and t.orderParentType = ?" + j++);
			paras.add(order_parent_type);
		}
		if (StringUtil.isNotEmpty(order_type)) {
			sb.append(" and t.orderType = ?" + j++);
			paras.add(order_type);
		}
		if (StringUtil.isNotEmpty(date)) {
			sb.append(" and str_to_date(t.tradeEndTime,'%Y-%m-%d %H:%i:%s') < str_to_date(?" + (j++)
					+ ",'%Y-%m-%d %H:%i:%s')");
			paras.add(date);
		}
		sb.append(" order by t.tradeEndTime desc");
		Query query = this.entityManager.createQuery(sb.toString());
		for (int i = 1; i < paras.size() + 1; i++) {
			query.setParameter(i, paras.get(i - 1));
		}
		query.setFirstResult((pageNo - 1) * pageSize);
		query.setMaxResults(pageSize);
		try {
			return query.getResultList();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public TradeRecord findByOrderNo(String orderNo) {
		List<TradeRecord> list = this.findByQuery("from TradeRecord t where t.orderNo = ?1 and t.isDeleted='1'",
				orderNo);
		if (null != list && list.size() > 0) {
			return list.get(0);
		}
		return null;
	}

	@Override
	public List<TradeRecord> getAllByDate(String account_id, String order_parent_type, String order_type, String date,
			int pageNo, int pageSize, boolean isToDay) {
		StringBuffer sb = new StringBuffer("from TradeRecord t where t.isDeleted='1' ");
		int j = 1;
		List<Object> paras = new ArrayList<Object>();
		if (StringUtil.isNotEmpty(account_id)) {
			sb.append(" and t.accId = ?" + j++);
			paras.add(account_id);
		}
		if (StringUtil.isNotEmpty(order_parent_type)) {
			sb.append(" and t.orderParentType = ?" + j++);
			paras.add(order_parent_type);
		}
		if (StringUtil.isNotEmpty(order_type)) {
			sb.append(" and t.orderType = ?" + j++);
			paras.add(order_type);
		}
		if (isToDay) {
			if (StringUtil.isNotEmpty(date)) {
				sb.append(" and str_to_date(t.tradeEndTime,'%Y-%m-%d') = str_to_date(?" + (j++) + ",'%Y-%m-%d')");
				paras.add(date);
			}
		}

		if (StringUtil.isNotEmpty(date)) {
			sb.append(" and str_to_date(t.tradeEndTime,'%Y-%m-%d %H:%i:%s') < str_to_date(?" + (j++)
					+ ",'%Y-%m-%d %H:%i:%s')");
			paras.add(date);
		}
		sb.append(" order by t.tradeEndTime desc");
		Query query = this.entityManager.createQuery(sb.toString());
		for (int i = 1; i < paras.size() + 1; i++) {
			query.setParameter(i, paras.get(i - 1));
		}
		if (!isToDay) {
			query.setFirstResult((pageNo - 1) * pageSize);
			query.setMaxResults(pageSize);
		}
		try {
			return query.getResultList();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public TradeRecord findByTradeSno(String tradeSno) {
		String hql = "from TradeRecord t where t.tradeSno = ?1 and t.isDeleted='1'";
		List<TradeRecord> findByQuery = this.findByQuery(hql, tradeSno);
		if (findByQuery != null && findByQuery.size() > 0) {
			return findByQuery.get(0);
		}
		return null;
	}

	@Override
	public void deleteByTranSno(String tradeSno) {
		String sql = "update m_base_wxxnk_trade_record set is_deleted=" + false + " where trade_sno='" + tradeSno + "'";
		Query query = this.entityManager.createNativeQuery(sql);
		query.executeUpdate();
	}

	@Override
	public Double getComsumDiscount(String otherCondition) {
		String sql = "select (sum(need_pay)/100-sum(real_pay)/100) from m_base_wxxnk_trade_record where 1=1 ";
		if (StringUtil.isNotEmpty(otherCondition)) {
			sql += otherCondition;
		}
		System.out.println(sql);
		Query query = this.entityManager.createNativeQuery(sql);
		List<Object> list = query.getResultList();
		Double discount = 0.0;
		if (null != list && list.size() > 0) {
			if (null != list.get(0)) {
				discount = Double.parseDouble((BigDecimal) list.get(0) + "");
			}
		}
		return discount;
	}

	@Override
	public List<TradeRecord> findTradeRecordList(String account_id, String cardNos, String order_parent_type,
			String order_type, String date, int pageNo, int pageSize, boolean isToDay) {
		StringBuffer sb = new StringBuffer("from TradeRecord t where t.isDeleted='1' ");
		int j = 1;
		List<Object> paras = new ArrayList<Object>();
		if (StringUtil.isNotEmpty(account_id)) {
			sb.append(" and t.accId = ?" + j++);
			paras.add(account_id);
		}
		if (StringUtil.isNotEmpty(order_parent_type)) {
			sb.append(" and t.orderParentType = ?" + j++);
			paras.add(order_parent_type);
		}
		if (StringUtil.isNotEmpty(order_type)) {
			sb.append(" and t.orderType = ?" + j++);
			paras.add(order_type);
		}
		if (isToDay) {
			if (StringUtil.isNotEmpty(date)) {
				sb.append(" and str_to_date(t.tradeEndTime,'%Y-%m-%d') = str_to_date(?" + (j++) + ",'%Y-%m-%d')");
				paras.add(date);
			}
		}

		if (StringUtil.isNotEmpty(date)) {
			sb.append(" and str_to_date(t.tradeEndTime,'%Y-%m-%d %H:%i:%s') < str_to_date(?" + (j++)
					+ ",'%Y-%m-%d %H:%i:%s')");
			paras.add(date);
		}
		sb.append(" and t.cardNo in(" + StringUtil.getChangeString(cardNos) + ")");
		sb.append(" order by t.tradeEndTime desc");
		Query query = this.entityManager.createQuery(sb.toString());
		for (int i = 1; i < paras.size() + 1; i++) {
			query.setParameter(i, paras.get(i - 1));
		}
		if (!isToDay) {
			query.setFirstResult((pageNo - 1) * pageSize);
			query.setMaxResults(pageSize);
		}
		try {
			return query.getResultList();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

}
